﻿using System;
using System.Net.Sockets;

namespace Kay.Communication.Framework.Base
{
    /// <summary>
    /// Tcp 类型的服务端
    /// </summary>
    public class TcpIPv4Server : IDisposable
    {
        private bool _disposed;

        private Socket _socket;

        /// <summary>
        /// 初始化新的实例对象
        /// </summary>
        public TcpIPv4Server() { }

        ~TcpIPv4Server()
        {
            Dispose(false);
        }

        /// <inheritdoc />
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// 释放由 System.Net.Sockets.Socket 使用的非托管资源，并可根据需要释放托管资源。
        /// </summary>
        /// <param name="disposing">如果为 true，则释放托管资源和非托管资源；如果为 false，则仅释放非托管资源。</param>
        protected void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }
            if (disposing)
            {
                if (_socket != null)
                {
                    ((IDisposable)_socket).Dispose();
                }
            }
            _disposed = true;
        }
    }
}